package com.demo;

import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * <code>ReentrantLockTest</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-06-12 13:51
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class ReentrantLockTest {
    public static void main(String[] args) throws InterruptedException {
        final ReentrantLock reentrantLock = new ReentrantLock();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    reentrantLock.lock();
                    System.out.println("线程1获取到了锁");
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    reentrantLock.unlock();
                }
            }
        }).start();

        Thread.sleep(1000);

        new Thread(new Runnable() {
            @Override
            public void run() {
                reentrantLock.lock();
                System.out.println("线程2获取到了锁");
                reentrantLock.unlock();
            }
        }).start();
    }
}
/*
这种写法会有问题
线程1获取到锁，要等5秒才会释放
线程2需要等5秒后线程1释放锁才可以获取锁，等的过程中，线程2是阻塞的

lock尽量不用

*/